package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.Contract;
import com.asm.repository.ContractRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.ContractVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author michael
 */
@Service
public class ContractService {
    @Resource
    private ContractRepository contractRepository;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取合同列表
     *
     * @param page 获取合同的对象
     * @return 存储合同的列表对象
     */
    private List<ContractVo> getContractVoList(Page<Contract> page) {
        List<ContractVo> list = new ArrayList<>();
        for (Contract contract : page.getContent()) {
            ContractVo contractVo = new ContractVo();
            BeanUtils.copyProperties(contract, contractVo);
            list.add(contractVo);
        }
        return list;
    }

    /**
     * 查询所有合同信息(不是前端需要的)
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同信息
     */
    public PageResult<ContractVo> findAllContracts(Integer pageNumber, Integer pageSize) {
        Page<Contract> page = contractRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), getContractVoList(page));
    }

    /**
     * 查询所有合同信息
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 所有合同信息
     */
    public PageResult<Map<String, Object>> findAllContractMsgs(Integer pageNumber, Integer pageSize) {
        Page<Map<String, Object>> page = contractRepository.findAllContractMsgs(PageRequest.of(pageNumber, pageSize));
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), page.getContent());
    }

    /**
     * 根据业务员编号查询所有合同信息
     *
     * @param sellId     业务员编号
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同信息
     */
    public PageResult<Map<String, Object>> findContractMsgsBySellId(Long sellId, Integer pageNumber, Integer pageSize) {
        Page<Map<String, Object>> page = contractRepository.findContractMsgsBySellId(sellId, PageRequest.of(pageNumber, pageSize));
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), page.getContent());
    }

    /**
     * 根据顾客编号查询所有合同信息
     *
     * @param customerId 顾客编号
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同信息
     */
    public PageResult<Map<String, Object>> findContractMsgsByCustomerId(Long customerId, Integer pageNumber, Integer pageSize) {
        Page<Map<String, Object>> page = contractRepository.findContractMsgsByCustomerId(customerId, PageRequest.of(pageNumber, pageSize));
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), page.getContent());
    }

    /**
     * 根据合同编号查询并返回合同信息
     *
     * @param id 合同编号
     * @return 合同vo对象
     */
    public ContractVo findContractById(Long id) {
        Contract contract = contractRepository.findContractById(id);
        ContractVo contractVo = new ContractVo();
        BeanUtils.copyProperties(contract, contractVo);
        return contractVo;
    }

    /**
     * 根据合同名字分页查询合同
     *
     * @param name       合同名字
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 合同
     */
    public PageResult<ContractVo> findContractByContractName(String name, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<Contract> contracts = contractRepository.findByContractName(name, page);
        return new PageResult<>(pageNumber, pageSize, contracts.getTotalPages(), getContractVoList(contracts));
    }

    /**
     * 添加合同
     *
     * @param contractVo 合同vo对象
     */
    @Transactional(rollbackFor = Exception.class)
    public Long addContract(ContractVo contractVo) {
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractVo, contract);
        contract.setId(snowflakeIdWorker.nextId());
        contract.setCreateAt(new Date());
        contract.setCreateBy(1L);
        contract.setUpdateAt(new Date());
        contract.setUpdateBy(1L);
        contractRepository.save(contract);
        return contract.getId();
    }

    /**
     * 更新合同信息
     *
     * @param contractVo 合同vo对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateContract(ContractVo contractVo) {
        Contract contract = contractRepository.findById(contractVo.getId()).get();
        BeanUtils.copyProperties(contractVo, contract);
        contract.setUpdateBy(1L);
        contract.setUpdateAt(new Date());
        contractRepository.save(contract);
    }

    /**
     * 删除指定的合同
     *
     * @param id 合同编号
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteContract(Long id) {
        Contract contract = contractRepository.findById(id).get();
        contract.setIsDel(true);
        contract.setUpdateBy(1L);
        contract.setUpdateAt(new Date());
        contractRepository.save(contract);
    }

}
